package edu.pku.sei.transformation.qvt.action;

import java.io.FileWriter;
import java.io.IOException;

import edu.pku.sei.transformation.editor.model.AtomicTransformation;
import edu.pku.sei.transformation.editor.model.implement.ICodegenElement;
import edu.pku.sei.transformation.qvt.adapter.standardqvt.TransformationAdapter;
import edu.pku.sei.transformation.qvt.codegen.QVTGenerator;

public class StandardQVTCodegenAdapter extends QVTCodegenAdapter {
	public static final String LANGUAGE = "QVT";

	@Override
	public String getLanguage() {
		return LANGUAGE;
	}

	@Override
	public boolean generateCodeToFile(ICodegenElement element, String file) {
		if (!canGenerate(element)) {
			return false;
		} else {
			AtomicTransformation transformation = getAtomicTransformation(element);
			QVTGenerator generator = new QVTGenerator();
			String content = generator.generate(new TransformationAdapter(
					transformation));
	
			FileWriter fw;
			try {
				fw = new FileWriter(file);
				fw.write(content, 0, content.length());
				fw.flush();
				fw.close();
				return true;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
	}

}
